<?php

if (isset($_REQUEST['client'])){
    $_SESSION['home_client'] = $_REQUEST['client'];
    base_obj::redirect(ROOT . "?input=main");
}

if (!isset($_SESSION['home_client']))
    $_SESSION['home_client'] = $_SESSION['user']['profile']['client'];

$client_obj = new client_obj();
$user_obj = new user_obj();
$record_obj = new record_obj();
$tariff_obj = new tariff_obj();
$task_obj = new task_obj();

$client_data = $client_obj->getClient($_SESSION['home_client']);

$filter = array();
$filter['client'] = $_SESSION['home_client'];
$filter['active'] = 1;
$client_users = $user_obj->getUsers("*", $filter);

$typeOfCooperation = array();
if (!empty($client_data['typeOfCooperation']))
    $typeOfCooperation = $client_obj->getClientTypeOfCooperation($client_data['typeOfCooperation']);

if (!empty($client_data['contactPerson']))
    $contactUser = $user_obj->getUserById($client_data['contactPerson']);
else {
    $contactUser = array(
        'surname' => ''
    );
}

$currentMonth = date("n");
$currentYear = date("Y");
$current = date("U");

$last = date("U") - date("t") * 24 * 60 * 60;
$lastMonth = date("n", $last);
$lastYear = date("Y", $last);

$filter = array();
$filter['month'] = $currentMonth;
$filter['year'] = $currentYear;
$filter['client'] = $_SESSION['home_client'];
$filter['nottype'] = 8;
$records_current = $record_obj->getListOfRecords($filter);
$current_sum = 0;
foreach ($records_current as $key => $record) {
    $records_current[$key]['date_formated'] = base_obj::convertDateTo($record['date'], 'dd/mm/yyyy H:i:s', 'datetime');
    $user = $user_obj->getUserById($record['user_id']);
    if (error_obj::isError($user) || empty($user)){
        $records_current[$key]['user_info'] = array("users_id" => 0);
    } else {
        $records_current[$key]['user_info'] = $user;
    }//
    $records_current[$key]['type_info'] = $record_obj->getTypeOfRecordById($record['type']);
    if ($record['tariff']!=-1)
        $records_current[$key]['tariff_info'] = $tariff_obj->getTariffById($record['tariff']);
    else
        $records_current[$key]['tariff_info']['title'] = '-';
    $current_sum += $record['amount'];
}

$overrange = $current_sum - $client_data['monthlyPrepaid'];
if ($overrange < 0)
    $overrange = 0;

$filter = array();
$filter['month'] = $currentMonth;
$filter['year'] = $currentYear;
$filter['client'] = $_SESSION['home_client'];
$filter['type'] = 8;
$goods_current = $record_obj->getListOfRecords($filter);
$current_goods_sum = 0;
foreach ($goods_current as $key => $record) {
    $goods_current[$key]['date_formated'] = base_obj::convertDateTo($record['date'], 'dd/mm/yyyy H:i:s', 'datetime');
    $user = $user_obj->getUserById($record['user_id']);
    if (error_obj::isError($user) || empty($user)){
        $goods_current[$key]['user_info'] = array("users_id" => 0);
    } else {
        $goods_current[$key]['user_info'] = $user;
    }//
    $goods_current[$key]['type_info'] = $record_obj->getTypeOfRecordById($record['type']);
    if ($record['tariff']!=-1)
        $goods_current[$key]['tariff_info'] = $tariff_obj->getTariffById($record['tariff']);
    else
        $goods_current[$key]['tariff_info']['title'] = '-';
    $current_goods_sum += $record['amount'];
}

$filter = array();
$filter['month'] = $currentMonth;
$filter['year'] = $currentYear;
$filter['client'] = $_SESSION['home_client'];
$filter['type'] = 3;
$transport_current = $record_obj->getListOfRecords($filter);
$current_transport_sum = 0;
foreach ($transport_current as $key => $record) {
    $transport_current[$key]['date_formated'] = base_obj::convertDateTo($record['date'], 'dd/mm/yyyy H:i:s', 'datetime');
    $user = $user_obj->getUserById($record['user_id']);
    if (error_obj::isError($user) || empty($user)){
        $transport_current[$key]['user_info'] = array("users_id" => 0);
    } else {
        $transport_current[$key]['user_info'] = $user;
    }//
    $transport_current[$key]['type_info'] = $record_obj->getTypeOfRecordById($record['type']);
    if ($record['tariff']!=-1)
        $transport_current[$key]['tariff_info'] = $tariff_obj->getTariffById($record['tariff']);
    else
        $transport_current[$key]['tariff_info']['title'] = '-';
    $current_transport_sum += $record['amount'];
}

$filter = array();
$filter['month'] = $lastMonth;
$filter['year'] = $lastYear;
$filter['client'] = $_SESSION['home_client'];
$filter['nottype'] = 8;
$records_last = $record_obj->getListOfRecords($filter);
$last_sum = 0;
foreach ($records_last as $key => $record) {
    $records_last[$key]['date_formated'] = base_obj::convertDateTo($record['date'], 'dd/mm/yyyy H:i:s', 'datetime');
    $user = $user_obj->getUserById($record['user_id']);
    if (error_obj::isError($user) || empty($user)){
        $records_last[$key]['user_info'] = array("users_id" => 0);
    } else {
        $records_last[$key]['user_info'] = $user;
    }//
    $records_last[$key]['type_info'] = $record_obj->getTypeOfRecordById($record['type']);
    if ($record['tariff']!=-1)
        $records_last[$key]['tariff_info'] = $tariff_obj->getTariffById($record['tariff']);
    else
        $records_last[$key]['tariff_info']['title'] = '-';
    $last_sum += $record['amount'];
}

$filter = array();
$filter['month'] = $lastMonth;
$filter['year'] = $lastYear;
$filter['client'] = $_SESSION['home_client'];
$filter['type'] = 8;
$goods_last = $record_obj->getListOfRecords($filter);
$last_goods_sum = 0;
foreach ($goods_last as $key => $record) {
    $goods_last[$key]['date_formated'] = base_obj::convertDateTo($record['date'], 'dd/mm/yyyy H:i:s', 'datetime');
    $user = $user_obj->getUserById($record['user_id']);
    if (error_obj::isError($user) || empty($user)){
        $goods_last[$key]['user_info'] = array("users_id" => 0);
    } else {
        $goods_last[$key]['user_info'] = $user;
    }//
    $goods_last[$key]['type_info'] = $record_obj->getTypeOfRecordById($record['type']);
    if ($record['tariff']!=-1)
        $goods_last[$key]['tariff_info'] = $tariff_obj->getTariffById($record['tariff']);
    else
        $goods_last[$key]['tariff_info']['title'] = '-';
    $last_goods_sum += $record['amount'];
}

$filter = array();
$filter['month'] = $lastMonth;
$filter['year'] = $lastYear;
$filter['client'] = $_SESSION['home_client'];
$filter['type'] = 3;
$transport_last = $record_obj->getListOfRecords($filter);
$last_transport_sum = 0;
foreach ($transport_last as $key => $record) {
    $transport_last[$key]['date_formated'] = base_obj::convertDateTo($record['date'], 'dd/mm/yyyy H:i:s', 'datetime');
    $user = $user_obj->getUserById($record['user_id']);
    if (error_obj::isError($user) || empty($user)){
        $transport_last[$key]['user_info'] = array("users_id" => 0);
    } else {
        $transport_last[$key]['user_info'] = $user;
    }//
    $transport_last[$key]['type_info'] = $record_obj->getTypeOfRecordById($record['type']);
    if ($record['tariff']!=-1)
        $transport_last[$key]['tariff_info'] = $tariff_obj->getTariffById($record['tariff']);
    else
        $transport_last[$key]['tariff_info']['title'] = '-';
    $last_transport_sum += $record['amount'];
}

$filter = array();
$filter['client'] = $_SESSION['home_client'];
$filter['status'] = 1;
$order = array(
    'col' => 'created',
    'orderBy' => 'created',
    'order' => 'ASC',
    'table' => '',
    'connector' => ''
); 
$tasks = $task_obj->getListOfTasks($filter, $order);
foreach ($tasks as $key => $task) {
    $tasks[$key]['creator_info'] = $user_obj->getUserById($task['creator'], array('name', 'surname'));
    $tasks[$key]['deadline_format'] = base_obj::convertDateTo($task['deadline'], 'dd/mm/yyyy', 'date');
    if ($tasks[$key]['deadline_format'] == MAX_DEADLINE)
        $tasks[$key]['deadline_format'] = '00/00/0000';
    $tasks[$key]['created_format'] = base_obj::convertDateTo($task['added'], 'dd/mm/yyyy H:i:s', 'datetime');
    $tasks[$key]['responsible_info'] = $user_obj->getUserById($task['responsible'], array('name', 'surname'));
    if ($task['worker'] != -1)
        $tasks[$key]['worker_info'] = $user_obj->getUserById($task['worker'], array('name', 'surname'));    
}

$GLOBALS['template_data']['date1'] = date("F") . " " . $currentYear;
$GLOBALS['template_data']['date2'] = date("F", $last) . " " . $lastYear;
$GLOBALS['template_data']['client_data'] = $client_data;
$GLOBALS['template_data']['client_users'] = $client_users;
$GLOBALS['template_data']['type_of_coop'] = $typeOfCooperation;
$GLOBALS['template_data']['contact_user'] = $contactUser;
$GLOBALS['template_data']['records_current'] = $records_current;
$GLOBALS['template_data']['goods_current'] = $goods_current;
$GLOBALS['template_data']['current_sum'] = $current_sum;
$GLOBALS['template_data']['transport_current'] = $transport_current;
$GLOBALS['template_data']['current_goods_sum'] = $current_goods_sum;
$GLOBALS['template_data']['current_transport_sum'] = $current_transport_sum;
$GLOBALS['template_data']['records_last'] = $records_last;
$GLOBALS['template_data']['goods_last'] = $goods_last;
$GLOBALS['template_data']['transport_last'] = $transport_last;
$GLOBALS['template_data']['last_sum'] = $last_sum;
$GLOBALS['template_data']['last_goods_sum'] = $last_goods_sum;
$GLOBALS['template_data']['last_transport_sum'] = $last_transport_sum;
$GLOBALS['template_data']['tasks'] = $tasks;
$GLOBALS['template_data']['overrange'] = $overrange;
?>
